﻿using SQLite;
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;
using YellowMap.Resources;

namespace YellowMap
{
    public class AppViewModel
    {
        public LanguageViewModel LanguageVM { get; set; }
        public SettingViewModel SettingVM { get; set; }
        public List<CategoryModel> Categories { get; set; }
        public FavoriteViewModel FavoriteVM { get; set; }
        public HistoryViewModel HistoryVM { get; set; }
        public List<MapType> MapTypes { get; set; }
       
        public Dictionary<string, int> SubCateId { get; set; }

        public AppViewModel()
        {

            // Init SubcateId
            SubCateId = new Dictionary<string, int>();

            
            LanguageVM = new LanguageViewModel();
            SettingVM = new SettingViewModel();
            Categories = new List<CategoryModel>();
            FavoriteVM = new FavoriteViewModel();
            HistoryVM = new HistoryViewModel();
            MapTypes = new List<MapType>();
            MapTypes.Add(new MapType() 
            { 
                Id = Constant.MAPTYPE_ROADMAP
            });

            MapTypes.Add(new MapType()
            {
                Id = Constant.MAPTYPE_AERIAL
            });

            MapTypes.Add(new MapType()
            {
                Id = Constant.MAPTYPE_HYBRID
            });

            MapTypes.Add(new MapType()
            {
                Id = Constant.MAPTYPE_TERRAIN
            });
        }
        #region Singleton
        static AppViewModel _instance = null;
        public static AppViewModel Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new AppViewModel();
                }

                return _instance;
            }
        }
        #endregion


        public static void Init()
        {
            GetAllCategoryData();
            CreateDatabase();
        }

        public static async void CreateDatabase()
        {
            IsolatedStorageFile isFile = IsolatedStorageFile.GetUserStoreForApplication();
            if (isFile.FileExists(Constant.DB_NAME))
                return;
            SQLiteAsyncConnection conn = new SQLiteAsyncConnection(Constant.DB_NAME);
            await conn.CreateTableAsync<History>();
            await conn.CreateTableAsync<Favorite>();
            await conn.CreateTableAsync<Place>();
            await conn.CreateTableAsync<Detail>();
        }


        static int compareSubCate(YellowMap.CategoryModel.SubCate sub1, YellowMap.CategoryModel.SubCate sub2)
        {
            return sub1.Order - sub2.Order;
        }

        static int compareCate(YellowMap.CategoryModel cate1, YellowMap.CategoryModel cate2)
        {
            return cate1.Order - cate2.Order;
        }

        static void GetAllCategoryData()
        {
            string xml = CommonX.ReadFileInProject(Constant.CATEGORY_FILE_PATH);
            XDocument xdoc = XDocument.Parse(xml).Document;
            var cates = xdoc.Root.Elements("category");//.Elements("category)");//.ToList();

            foreach (var cate in cates)
            {
                string cate_id, cate_color, cate_order, cate_icon;
                cate_id = cate.Attribute("id").Value;
                cate_color = cate.Attribute("color").Value;
                cate_order = cate.Attribute("order").Value;
                cate_icon = cate.Attribute("icon").Value;
                CategoryModel category = new CategoryModel();
                category.Color = cate_color;
                category.Icon = cate_icon;
                category.Id = cate_id;
                category.Order = byte.Parse(cate_order);
                AppViewModel.Instance.Categories.Add(category);

                var subCates = cate.Elements("sub");
                int subCateIndex = 0;
                foreach (var subCate in subCates)
                {
                    string sub_cate_id, sub_cate_type, sub_cate_order, sub_cate_keyword;
                    sub_cate_id = subCate.Attribute("id").Value;
                    sub_cate_type = subCate.Attribute("type").Value;
                    sub_cate_order = subCate.Attribute("order").Value;
                    sub_cate_keyword = subCate.Attribute("keyword").Value;
                    AppViewModel.Instance.SubCateId.Add(sub_cate_id, subCateIndex++);
                    category.SubCategories.Add(new CategoryModel.SubCate() { 
                        CateParent = category,
                        Type = sub_cate_type,
                        Id = sub_cate_id,
                        Keyword = sub_cate_keyword,
                        Order = byte.Parse(sub_cate_order)
                        
                    });
                }
                category.SubCategories.Sort(compareSubCate);
        
            }

            AppViewModel.Instance.Categories.Sort(compareCate);

         
        }
    }
}
